package com.hyd.project;

import java.io.File;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.hyd.application.MyApplication;
import com.hyd.service.MusicService;
import com.hyd.util.RecordingUtil;
import com.hyd.util.ToBlur;
import com.hyd.view.RippleView;
/**
 * 绘本试读
 * @author llf
 *
 */
public class ProbationActivity extends Activity {
	private AlertDialog uploadDialog;
	private RelativeLayout dialogView;
	private ImageView backgroud;
	// 播放器相关
	private RelativeLayout player_view;
	private ProgressBar progress;
	private Handler handler, uploadHandler;
	private Runnable runnable, uploadRunnable;
	private ImageView play, loop;
	private RippleView playRipple, loopRipple, nextRipple, priorRipple;
	// 录音相关
	private RecordingUtil recordingUtil;
	private ImageView probation_image, probation_play;
	private boolean isSDCardExit, isFirst = true, islike;
	private int recordingStatus = 0, status = 0;
	private ProgressBar probation_progress;
	private Button upload, recording;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_probation);

		initView();
		initClick();
	}

	private void initView() {
		backgroud = (ImageView) findViewById(R.id.backgroud);
		dialogView = (RelativeLayout) LayoutInflater.from(this).inflate(
				R.layout.probation_upload_view, null);
		probation_play = (ImageView) dialogView.findViewById(R.id.upload_play);
		probation_progress = (ProgressBar) dialogView
				.findViewById(R.id.upload_progress);
		upload = (Button) dialogView.findViewById(R.id.upload);
		recording = (Button) dialogView.findViewById(R.id.rerecording);
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setView(dialogView);
		uploadDialog = builder.create();

		probation_image = (ImageView) findViewById(R.id.probation_image);
		// 播放器相关
		player_view = (RelativeLayout) findViewById(R.id.player_view);
		play = (ImageView) player_view.findViewById(R.id.play);
		loop = (ImageView) player_view.findViewById(R.id.loop);
		playRipple = (RippleView) player_view.findViewById(R.id.play_ripple);
		loopRipple = (RippleView) player_view.findViewById(R.id.loop_ripple);
		nextRipple = (RippleView) player_view.findViewById(R.id.next_ripple);
		priorRipple = (RippleView) player_view.findViewById(R.id.prior_ripple);
		progress = (ProgressBar) player_view.findViewById(R.id.seek);

		handler = new Handler();
		uploadHandler = new Handler();
		runnable = new Runnable() {

			@Override
			public void run() {
				if (MyApplication.playstatus == 1) {
					progress.setMax(MusicService.player.getDuration());
					progress.setProgress(MusicService.player
							.getCurrentPosition());
				}
				// 延迟启动线程，主要是为了保证启动线程的时候,播放音乐的服务已经启动
				handler.postDelayed(runnable, 50);
			}
		};

		if (MusicService.player != null && MyApplication.playstatus == 1) {
			play.setBackgroundResource(R.drawable.player_hesitation);
			handler.post(runnable);
		} else if (MusicService.player != null && MyApplication.playstatus == 0) {
			progress.setMax(MusicService.player.getDuration());
			progress.setProgress(MusicService.player.getCurrentPosition());
		}

		uploadRunnable = new Runnable() {

			@Override
			public void run() {
				if (status == 1) {
					probation_progress
							.setMax(MusicService.player.getDuration());
					probation_progress.setProgress(MusicService.player
							.getCurrentPosition());
				}
				uploadHandler.postDelayed(uploadRunnable, 50);
			}
		};
		Bitmap bitmap = ToBlur.convertToBlur(BitmapFactory.decodeResource(
				getResources(), R.drawable.man));
		backgroud.setImageBitmap(bitmap);
		// 录音相关
		recordingUtil = new RecordingUtil();
	}

	private void initClick() {
		playRipple.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (MyApplication.playstatus == 0) {
					play.setBackgroundResource(R.drawable.player_hesitation);
					MyApplication.playstatus = 1;
					MusicService.goon();
					handler.post(runnable);
				} else {
					MusicService.pause();
					MyApplication.playstatus = 0;
					play.setBackgroundResource(R.drawable.play);

				}
			}
		});
		// 设置为循环播放
		loopRipple.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (MyApplication.isLoop) {
					loop.setBackgroundResource(R.drawable.unloop);
					MusicService.player.setLooping(false);
					MyApplication.isLoop = false;
				} else {
					loop.setBackgroundResource(R.drawable.loop);
					MusicService.player.setLooping(true);
					MyApplication.isLoop = true;
				}
			}
		});

		nextRipple.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				play.setBackgroundResource(R.drawable.player_hesitation);
				MyApplication.playstatus = 1;
				handler.post(runnable);
				MusicService.playNext();
			}
		});

		priorRipple.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				play.setBackgroundResource(R.drawable.player_hesitation);
				MyApplication.playstatus = 1;
				handler.post(runnable);
				MusicService.playPrior();
			}
		});

		// 播放录音
		probation_play.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (isFirst) {
					MusicService.audition(Environment
							.getExternalStorageDirectory().getAbsolutePath()
							+ File.separator + "1.mp3");
					isFirst = false;
					probation_play
							.setBackgroundResource(R.drawable.probation_upload_lull);
					uploadHandler.post(uploadRunnable);
					status = 1;
				} else {
					if (status == 0) {
						probation_play
								.setBackgroundResource(R.drawable.probation_upload_lull);
						status = 1;
						MusicService.goon();
						uploadHandler.post(uploadRunnable);
					} else {
						MusicService.pause();
						status = 0;
						probation_play
								.setBackgroundResource(R.drawable.probation_upload_play);

					}
				}
			}
		});

		upload.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO 录音上传

			}
		});

		recording.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				probation_image
						.setBackgroundResource(R.drawable.probation_play);
				recordingStatus = 0;
				uploadDialog.dismiss();
				Toast.makeText(ProbationActivity.this, "已准备完毕，可以重新进行试读",
						Toast.LENGTH_SHORT).show();
			}
		});

		uploadDialog.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface dialog) {
				MusicService.pause();
				status = 0;
				isFirst = true;
			}
		});
	}

	// 点击重录按钮
	public void recording(View v) {
		isSDCardExit = Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED);
		if (isSDCardExit) {
			if (recordingStatus == 1) {
				probation_image
						.setBackgroundResource(R.drawable.probation_play);
				recordingStatus = 0;
				Toast.makeText(ProbationActivity.this, "已准备完毕，可以重新进行试读",
						Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(ProbationActivity.this, "您还没进行试读",
						Toast.LENGTH_SHORT).show();
			}
		} else {
			Toast.makeText(ProbationActivity.this, "请插入SD卡再进行相应的操作",
					Toast.LENGTH_SHORT).show();
		}
	}

	// 点击试读按钮
	public void probation(View v) {
		isSDCardExit = Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED);
		if (isSDCardExit) {
			if (recordingStatus == 0) {
				probation_image
						.setBackgroundResource(R.drawable.probation_lull);
				recordingUtil.startRecording();
				recordingStatus = 1;
				islike = false;
			} else {
				if (islike == false) {
					recordingUtil.destroy();
					islike = true;
				}
				uploadDialog.show();
				probation_progress.setProgress(0);
				progress.setProgress(0);
				isFirst = true;
				MusicService.pause();
				MyApplication.playstatus = 0;
				play.setBackgroundResource(R.drawable.play);
				probation_play
						.setBackgroundResource(R.drawable.probation_upload_play);
			}
		} else {
			Toast.makeText(ProbationActivity.this, "请插入SD卡再进行相应的操作",
					Toast.LENGTH_SHORT).show();
		}
	}

	public void otherProbation(View v) {
		Intent intent = new Intent(ProbationActivity.this,
				OtherProbationActivity.class);
		startActivity(intent);
		overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
	}

	public void back(View v) {
		finish();
		overridePendingTransition(android.R.anim.slide_in_left,
				android.R.anim.slide_out_right);
	}

	@Override
	protected void onDestroy() {
		handler.removeCallbacks(runnable);
		uploadHandler.removeCallbacks(uploadRunnable);
		recordingUtil.destroy();
		super.onDestroy();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		if (MusicService.player != null && MyApplication.playstatus == 1) {
			play.setBackgroundResource(R.drawable.player_hesitation);
			handler.post(runnable);
		} else if (MusicService.player != null && MyApplication.playstatus == 0) {
			progress.setMax(MusicService.player.getDuration());
			progress.setProgress(MusicService.player.getCurrentPosition());
		}
	}
}
